package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.Department;
import com.jrtc.base.entity.bo.FingerFuck;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.bo.Patientandfingerfuck;
import com.jrtc.base.entity.vo.FingerFuckDetailsVo;
import com.jrtc.base.entity.vo.FingerFuckVo;
import com.jrtc.dao.DepartmentDao;
import com.jrtc.dao.FingerFuckDao;
import com.jrtc.dao.PatientDao;
import com.jrtc.dao.PatientandfingerfuckDao;
import com.jrtc.service.DepartmentService;
import com.jrtc.service.FingerFuckService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service("fingerFuckService")
public class FingerFuckServiceImpl implements FingerFuckService {
    @Resource
    private FingerFuckDao fingerFuckDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private PatientandfingerfuckDao patientandfingerfuckDao;


    // 生成指定范围的随机数字【不重复】
    public List<Integer> randomNum(int scope, int total) {
        List<Integer> mylist = new ArrayList<>(); // 用于储存不重复的随机数
        Random rd = new Random();
        while (mylist.size() < scope) {
            int myNum = rd.nextInt(total);
            if (!mylist.contains(myNum += 1)) { // 判断容器中是否包含指定的数字
                mylist.add(myNum); // 往集合里面添加数据。
            }
        }
        return mylist;
    }

    /**
     * 患者获取手指操信息
     * */
    @Override
    public List<FingerFuckVo> fingerFuck(String idCard, String type) throws Exception {
        List<FingerFuckVo> fingerFuckVoList = new ArrayList<>();

        Patient patient = patientDao.needBallCount(idCard);
        if (patient == null){
            throw new Exception("证件号错误");
        }

        if (type.equals(PublicConstants.FingerFuckEnum.One.getValue())){
            List<Patientandfingerfuck> patientandfingerfucks = patientandfingerfuckDao.patientandfingerfuckList(patient.getPatientId());
            if (patientandfingerfucks != null && patientandfingerfucks.size() > 0){
                for (Patientandfingerfuck patientandfingerfuck : patientandfingerfucks) {
                    FingerFuckVo fingerFuckVo = new FingerFuckVo();
                    FingerFuckDetailsVo left = new FingerFuckDetailsVo();

                    FingerFuck fingerFuck = fingerFuckDao.FingerFuckById(patientandfingerfuck.getFingerfuckId());

                    if (fingerFuck != null){
                        left.setId(fingerFuck.getFingerFuckId());
                        left.setName(fingerFuck.getFingerFuckEnum());
                        left.setUrl(fingerFuck.getFingerFuckUrl());
                        fingerFuckVo.setLeft(left);
                    }
                    else {
                        throw new Exception("id:" + patientandfingerfuck.getFingerfuckId() + "出现错误");
                    }
                    fingerFuckVoList.add(fingerFuckVo);
                }
            }
            else {
                fingerFuckVoList = fingerFuckVo(type);
            }
        }
        else {
            List<Patientandfingerfuck> lefts = patientandfingerfuckDao.patientandfingerfucks(patient.getPatientId(), "left");
            List<Patientandfingerfuck> rights = patientandfingerfuckDao.patientandfingerfucks(patient.getPatientId(), "right");
            if ((lefts != null && lefts.size() > 0) && (rights == null || rights.size() <= 0)){
                rights = lefts;
            }
            else if ((lefts == null || lefts.size() <= 0) && (rights != null && rights.size() > 0)){
                lefts = rights;
            }
            else if ((lefts == null || lefts.size() <= 0) &&(rights == null || rights.size() <= 0)){
                fingerFuckVoList = fingerFuckVo(type);
                return fingerFuckVoList;
            }

            int count = lefts.size();

            //作用等同于i
            int j = 0;
            for (int i = 0; i < lefts.size(); i++) {
                FingerFuckVo fingerFuckVo = new FingerFuckVo();
                FingerFuckDetailsVo left = new FingerFuckDetailsVo();
                FingerFuckDetailsVo right = new FingerFuckDetailsVo();

                FingerFuck fingerFuckLeft = fingerFuckDao.FingerFuckById(lefts.get(i).getFingerfuckId());

                if (j >= rights.size()){
                    j = 0;
                }
                FingerFuck fingerFuckRight = fingerFuckDao.FingerFuckById(rights.get(j).getFingerfuckId());
                j++;

                left.setId(fingerFuckLeft.getFingerFuckId());
                left.setName(fingerFuckLeft.getFingerFuckEnum());
                left.setUrl(fingerFuckLeft.getFingerFuckUrl());
                fingerFuckVo.setLeft(left);

                right.setId(fingerFuckRight.getFingerFuckId());
                right.setName(fingerFuckRight.getFingerFuckEnum());
                right.setUrl(fingerFuckRight.getFingerFuckUrl());
                fingerFuckVo.setRight(right);

                fingerFuckVo.setLeft(left);
                fingerFuckVo.setRight(right);

                fingerFuckVoList.add(fingerFuckVo);

                if (j <= rights.size() && (i + 1) == lefts.size()){
                    i = -1;
                    count = rights.size();
                }

                if (fingerFuckVoList.size() == count){
                    break;
                }
            }
        }

        return fingerFuckVoList;
    }

    /**
     * 获取手指操随机个组的随机动作
     * */
    public List<FingerFuckVo> fingerFuckVo(String type) throws Exception {
        List<FingerFuckVo> fingerFuckVoList = new ArrayList<>();
        List<FingerFuck> fingerFucks = fingerFuckDao.FingerFucks();
        if (fingerFucks == null && fingerFucks.size() <= 0){
            throw new Exception("没有手指操信息");
        }

        //随机获取本次手指操共几组
        int count;
        int num = (int) (Math.random() * 10);
        if (num % 2 == 0){
            count = 5;
        }
        else {
            count = 3;
        }

        int total = 1;
        if (type.equals(PublicConstants.FingerFuckEnum.Two.getValue())){
            total = 2;
        }

        //本次手指操共几组就循环几次
        for (int j = 0 ; j < count ; j++){
            FingerFuckVo fingerFuckVo = new FingerFuckVo();
            //单手循环一次，双手循环两次
            for (int i = 0 ; i < total ; i++) {
                List<Integer> list = randomNum(count, fingerFucks.size() - 1);
                FingerFuck fingerFuck;
                if (i == 0){
                    fingerFuck = fingerFucks.get(list.get(j));

                    FingerFuckDetailsVo left = new FingerFuckDetailsVo();
                    left.setId(fingerFuck.getFingerFuckId());
                    left.setName(fingerFuck.getFingerFuckEnum());
                    left.setUrl(fingerFuck.getFingerFuckUrl());
                    fingerFuckVo.setLeft(left);
                }
                else {
                    int n = (int) (Math.random() * (fingerFucks.size() - 1));
                    fingerFuck = fingerFucks.get(n);

                    FingerFuckDetailsVo right = new FingerFuckDetailsVo();
                    right.setId(fingerFuck.getFingerFuckId());
                    right.setName(fingerFuck.getFingerFuckEnum());
                    right.setUrl(fingerFuck.getFingerFuckUrl());
                    fingerFuckVo.setRight(right);
                }
            }
            fingerFuckVoList.add(fingerFuckVo);
        }
        return fingerFuckVoList;
    }

}